package com.micro.ribbon.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @program: micro-service
 * @description: ribbon 测试
 * @author: Lefnmg
 * @create: 2018-12-09 16:59
 **/
@RestController
public class RibbonController {
    @Autowired
    RestTemplate restTemplate;

    /**
     * 使用熔断策略
     * @param name
     * @return
     */
    @HystrixCommand(fallbackMethod = "ribbonError")
    @GetMapping(value = "/ribbon")
    public String ribbon(@RequestParam String name) {
        return get(name);
    }

    public String get(String name) {
        return restTemplate.getForObject("http://base/ribbon?name=" + name, String.class);
    }

    public String ribbonError(String name) {
        return "ribbon," + name + ",sorry,error!";
    }
}
